package com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.category;

import com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.SalesPlanStatAtomAbstract;
import com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.model.SalesPlanStatRequest;
import com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.organization.*;
import com.alibaba.cz.base.tool.log.Log;

import java.util.concurrent.CountDownLatch;

/**
 * @author luyi
 * @date 2021/12/2 11:53 下午
 */
public class CategoryStatService extends SalesPlanStatAtomAbstract {

    private static final Log log = Log.getLogger(CategoryStatService.class);

    public CategoryStatService(SalesPlanStatRequest statRequest, CountDownLatch latch){
        super.statRequest = statRequest;
        super.latch = latch;
    }

    @Override
    protected void handle(SalesPlanStatRequest statRequest) {
        try {
            categoryCollect(statRequest);
        } catch (InterruptedException e) {
            log.error("CategoryStatService.InterruptedException:",e);
        }
    }

    @Override
    protected String taskName() {
        return "categoryStatService.gather";
    }

    /**
     * 类目维度汇总
     */
    public void categoryCollect(SalesPlanStatRequest statRequest) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        salesPlanTaskExecutor.execute(new CategoryEmployeeStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryDepartmentStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryChannelStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryOrganizationStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryBusinessUnitStatService(statRequest,countDownLatch));
        countDownLatch.await();
    }
}
